import requests
import re
import json
import schedule
import time
def get_data():
    response = requests.get("https://3g.dxy.cn/newh5/view/pneumonia?from=singlemessage&isappinstalled=0")
    response.encoding = response.apparent_encoding
    html = response.text
    data = list()                                                                           #目前rawdata是data
    data.append(dict())
    data[0]["provinceName"] = "中国"
    data[0]["continents"] = "亚洲"
    front_index = re.search("window.getStatisticsService = ", html).span()[1]
    back_index = front_index + 1
    stack = list('{')
    while stack != list():
        if html[back_index] == '{':
            stack.append(html[back_index])
        elif html[back_index] == '}':
            if stack[-1] == '{':
                stack.pop()
            else:
                stack.append(html[back_index])
        back_index += 1
    temp = json.loads(html[front_index:back_index])
    for t in ("confirmedCount", "suspectedCount", "curedCount", "deadCount", "seriousCount", "suspectedIncr"):
        data[0][t] = temp[t]
    front_index = re.search("window.getAreaStat = ", html).span()[1]
    back_index = front_index + 1
    stack = list('[')
    while stack != list():
        if html[back_index] == '[':
            stack.append(html[back_index])
        elif html[back_index] == ']':
            if stack[-1] == '[':
                stack.pop()
            else:
                stack.append(html[back_index])
        back_index += 1
    data[0]["cities"] = json.loads(html[front_index:back_index])
    front_index = re.search("window.getListByCountryTypeService2 = ", html).span()[1]
    back_index = front_index + 1
    stack = list('[')
    while stack != list():
        if html[back_index] == '[':
            stack.append(html[back_index])
        elif html[back_index] == ']':
            if stack[-1] == '[':
                stack.pop()
            else:
                stack.append(html[back_index])
        back_index += 1
    temp = json.loads(html[front_index:back_index])
    for country in temp:
        if country["provinceName"] != "中国":
            data.append(country)
    return data

def write_txt():
    time.localtime(time.time())
    path = '../dataset/every_hour_raw_data/'
    path2 = '../dataset/every_day_raw_data/'
    filename = ''
    for temp in (str(time.localtime(time.time()).tm_mon),
                 str(time.localtime(time.time()).tm_mday),
                 str(time.localtime(time.time()).tm_hour),
                 str(time.localtime(time.time()).tm_min),
                 str(time.localtime(time.time()).tm_sec)):
        if len(temp) == 1:
            temp = '0'+temp
        filename += temp
    filename += '.txt'
    with open(path + filename, 'w') as f:
        f.write(json.dumps(get_data(), indent = 4))
    if filename[4:6] == '00':
        with open(path2 + filename, 'w') as f:
            f.write(json.dumps(get_data(), indent = 4))
write_txt()
schedule.every().hour.do(write_txt)
while True:
    schedule.run_pending()
    time.sleep(1)